package com.fili.core;

import com.fili.gui.MainFrame;

import java.util.ArrayList;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.BufferedReader;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 6, 2006
 * Time: 1:10:34 PM
 * Revision: 0
 */
public class ExecThread extends Thread
{
    private int progress;
    private BatchJob batchJob;
    private MainFrame mf;


    public ExecThread(BatchJob batchJob, MainFrame mf)
    {
        this.batchJob = batchJob;
        this.mf = mf;
    }


    public void createList(String s, ArrayList al)
    {
        if (al.size() == 0)
        {
            try
            {
                FileOutputStream fos = new FileOutputStream("tmpmake.bat");
                fos.write(s.getBytes());

                fos.write("\n\n".getBytes());

                for (java.util.Iterator it = batchJob.commands.iterator(); it.hasNext();)
                {
                    String cmd = (String) it.next();
                    fos.write((cmd + "\n").getBytes());
                }

                fos.close();

                final Process p = Runtime.getRuntime().exec("tmpmake.bat");
/*                Thread t = new Thread(new Runnable()
                {

                    public void run()
                    {

                        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                        String line;
                        try
                        {
                            while ((line = input.readLine()) != null)
                            {
                                mf.addOutputText(line, false);
                            }
                            input.close();
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                });
                t.start();
                t = new Thread(new Runnable()
                {

                    public void run()
                    {

                        BufferedReader input = new BufferedReader(new InputStreamReader(p.getErrorStream()));
                        String line;
                        try
                        {
                            while ((line = input.readLine()) != null)
                            {
                                mf.addOutputText(line, true);
                            }
                            input.close();
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                });
                t.start();             */

                p.waitFor();

                mf.updateProgress(++progress);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        else
            if (al.get(0) instanceof ArrayList)
                for (int i = 2; i < al.size(); i++)
                {
                    createList(s + "set " + al.get(1) + "=" + al.get(i) + "\n", (ArrayList) (al.get(0)));
                }
    }

    public void run()
    {
        progress = 0;
        mf.updateProgress(0);
        createList("", batchJob.selections);
        mf.updateProgress(-1);
    }
}
